package UI

import (
	. "SGEngine"
	//"fmt"
)

type UIButton struct {
	*UINode
	DefaultTex *UITexture
	EnableTex  *UITexture
	normal     *UITexture
	hover      *UITexture
	press      *UITexture
	btnlabel   *FreetypeLabel

	ClickFunc func()
}

func NewButton() *UIButton {
	RBtn := new(UIButton)
	RBtn.UINode = NewUINode()

	f := NewFont("C:\\Windows\\Fonts\\msyh.ttf")
	RBtn.btnlabel = CreateFTLabel(f, "35", 12)
	RBtn.btnlabel.Color = NewColor3(0, 0, 0)
	RBtn.SetIsSizeScale(false)
	RBtn.SetSize(73, 20, 0)

	RBtn.normal = NewUITexture("source\\Skin\\Buttons\\button-normal.png")
	RBtn.hover = NewUITexture("source\\Skin\\Buttons\\button-pressed.png")
	RBtn.press = NewUITexture("source\\Skin\\Buttons\\button-prelight.png")
	RBtn.UINode.AnchorPoint = Vec2{0.5, 0.5}
	RBtn.hover.AnchorPoint = Vec2{0.5, 0.5}
	RBtn.normal.AnchorPoint = Vec2{0.5, 0.5}
	RBtn.press.AnchorPoint = Vec2{0.5, 0.5}

	RBtn.hover.SetSize(73, 20, 0)
	RBtn.normal.SetSize(73, 20, 0)
	RBtn.press.SetSize(73, 20, 0)
	RBtn.normal.Enable = true
	RBtn.hover.Enable = false
	RBtn.press.Enable = false

	AddChild(RBtn, RBtn.hover)
	AddChild(RBtn, RBtn.normal)
	AddChild(RBtn, RBtn.press)
	AddChild(RBtn, RBtn.btnlabel)

	RegisterEvent(RBtn, EVENT_MOUSRENTER, RBtn._MouseEnter)
	RegisterEvent(RBtn, EVENT_MOUSELEAVE, RBtn._MouseLevel)
	RegisterEvent(RBtn, EVENT_MOUSEDOWN, RBtn._MouseDown)
	RegisterEvent(RBtn, EVENT_MOUSEUP, RBtn._MouseUp)

	RegisterEvent(RBtn, EVENT_CLICK, RBtn._MouseClick)
	return RBtn
}

func (u *UIButton) _MouseEnter(X, Y int, node *EventNode) {
	btn := node.Mnode.(*UIButton)
	btn.normal.Enable = false
	btn.hover.Enable = true
	btn.press.Enable = false
}

func (u *UIButton) _MouseLevel(X, Y int, node *EventNode) {
	btn := node.Mnode.(*UIButton)
	btn.normal.Enable = true
	btn.hover.Enable = false
	btn.press.Enable = false
	btn.btnlabel.Color = BlackColor

}
func (u *UIButton) _MouseDown(X, Y int, node *EventNode) {
	btn := node.Mnode.(*UIButton)
	btn.normal.Enable = false
	btn.hover.Enable = false
	btn.press.Enable = true
	btn.btnlabel.Color = NewColor3(1, 1, 1)
}

func (u *UIButton) _MouseUp(X, Y int, node *EventNode) {
	btn := node.Mnode.(*UIButton)
	u._MouseLevel(X, Y, node)
	btn.btnlabel.Color = BlackColor
}

func (u *UIButton) _MouseClick(X, Y int, node *EventNode) {
	btn := node.Mnode.(*UIButton)
	if btn.ClickFunc != nil {
		btn.ClickFunc()
	}
}
